(define default-email-suffix
  (let ((parts (brl-split "." this-host)))
    (if (> (length parts) 1)
	(string-append "@" (brl-string-join "." (cdr parts)))
	"")))

; Change this if environment variable USER is not set
(define this-user default-user)

(define default-email (string-append this-user default-email-suffix))

; Change this if it doesn't set your e-mail address properly
(define this-email "lxy@msn.com")

; If you are not at MIT, change "outgoing.mit.edu" to "localhost"
; or (if that fails) to another host that accepts SMTP connections.
(define default-smtp-host "smtp.qq.com")



(define (base64-encode str)
  (make <string>
    (invoke-static <yuanma.sms.Base64>  'getBASE64 str)
     ))

(define (base64-decode str)
  (make <string>
    (invoke-static <yuanma.sms.Base64>  'getFromBASE64 str)
     ))
  


(define (auth-smtp-sender server this-host envelope user-id password)
  (lambda (to-list . objs)
    (if (not (list? to-list))
	(error "Recipient list not a list:" to-list))
    (let* ((mystr '()) (mystr1 "")
           (sock (brl-tcp-socket server 25))
	   (in (brl-tcp-in sock))
	   (out (brl-tcp-out sock))
	   (send1 (lambda (obj) (display obj out)))
	   (send (lambda x (for-each send1 x))))
      (call-with-input-string
       (apply brl-string objs)
       (lambda (strport)
	(set! mystr (append mystr (list  (brl-smtp-ok? sock))))
	 (send "EHLO " this-host brl-smtp-eol)
         (let mloop ((ret-str  (brl-smtp-ok? sock)))
           (if (brl-ends-with? "MIME" ret-str)
               ret-str
               (mloop (brl-smtp-ok? sock))))
         (send "AUTH LOGIN"  brl-smtp-eol)
         (set! mystr (append mystr (list  (brl-smtp-ok? sock))))
        
         (send (base64-encode user-id)  brl-smtp-eol)
         (set! mystr (append mystr (list  (brl-smtp-ok? sock))))
         (send (base64-encode password)  brl-smtp-eol)
         (set! mystr (append mystr (list  (brl-smtp-ok? sock))))
         
	 (send "MAIL FROM:<" envelope ">" brl-smtp-eol)
	 (brl-smtp-ok? sock)
	 (for-each
	  (lambda (rcpt)
	    (send "RCPT TO:<" rcpt ">" brl-smtp-eol)
	    (brl-smtp-ok? sock))
	  to-list)
	 (send "DATA" brl-smtp-eol)
	 (brl-smtp-ok? sock)
	 (send "Date: " (brl-smtp-date (make <java.util.Date>))
	       brl-smtp-eol)
	 ; Find/send first non-blank line
	 (let loop ((textline (brl-trim (read-line strport))))
	   (if (string=? textline "")
	       (loop (brl-trim (read-line strport)))
	       (send textline brl-smtp-eol)))
	 (let loop ((textline (read-line strport)))
	   (if (not (eof-object? textline))
	       (begin
		 (if (zero? (string-length textline))
		     (send1 brl-smtp-eol)
		     (begin
		       ; Escape with . lines starting with .
		       (if (equal? #\. (string-ref textline 0))
			   (send "."))
		       (send textline brl-smtp-eol)))
		 (loop (read-line strport)))))
	 (send1 brl-smtp-eom)
         (set! mystr (append mystr (list  (brl-smtp-ok? sock))))
;	 (brl-smtp-ok? sock)

	 (send "QUIT" brl-smtp-eol)
	 (brl-smtp-ok? sock)
	 (close-output-port out)
	 (close-input-port in)
	 mystr)
))))


(define mail
  (auth-smtp-sender
   "pub.zhuhai.gd.cn"
;   "smtp.qq.com"			; the SMTP server
   this-host				; the web server
   "lxyzyq@pub.zhuhai.gd.cn"				; bounced mail goes here
"lxyzyq@pub.zhuhai.gd.cn"
"12345abcde"
   ))

